<?php
App::uses('AppController', 'Controller');
/**
 * Services Controller
 *
 * @property Service $Service
 * @property PaginatorComponent $Paginator
 * @property SessionComponent $Session
 */
class ServicesController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator', 'Session');
	/*
     public function beforeFilter() {
        $this->Security->unlockedActions = array('add');
        parent::beforeFilter();
		}
		*/
/**
 * index method
 *
 * @return void
 */
	public function index() {
		//$this->set('saved', false);
		$cate =  $this->Service->CateService->find('list',array('order'=>'CateService.id ASC'));
		$this->set(compact('cate'));
	}
	
	public function listservice($id){
		$list = $this->Service->find('all',array('conditions'=>array('cate_services_id'=>$id),'order'=>'Service.id ASC'));
		return $list;
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Service->exists($id)) {
			throw new NotFoundException(__('Invalid service'));
		}
		$options = array('conditions' => array('Service.' . $this->Service->primaryKey => $id));
		$this->set('service', $this->Service->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
	$this->set('saved', false); //false by default - controls closure of overlay in which this is opened  
	$data = array();	
		if ($this->request->is('post')) {
			$price = str_replace(',', '', $this->request->data['Service']['gia']);	
			$data['Service']['name']= $this->request->data['Service']['name'];
			$data['Service']['price']= $price;
			$data['Service']['unit']= $this->request->data['Service']['unit'];
			$data['Service']['note']= $this->request->data['Service']['note'];
			$data['Service']['cate_services_id']= $this->request->data['Service']['cate_services_id'];
			$data['Service']['status']= $this->request->data['Service']['status'];
			$data['Service']['orders']= $this->request->data['Service']['thutu'];
			$this->Service->create();
			if ($this->Service->save($data)) {
				$this->set('saved', true);
			} else {
				//$this->Session->setFlash(__('Chưa lưu được. Hãy thử lại.'));
			}
		}
		$cateServices = $this->Service->CateService->find('list');
		$this->set(compact('cateServices'));
		
	}
	
	public function countservice($id = null){
		$this->loadModel('ImportService');
		$count =  $this->ImportService->find('all',array('conditions'=>array('services_id'=>$id),'fields'=>array('count')));
		$total = 0;
		if($count !=null){
			foreach($count as $c){
				$total = $total + $c['ImportService']['count'];
				
			}
		}
		return $total;
	}
	
	public function import($id = null){
		//$this->autoRender = false;	
		$this->loadModel('ImportService');
		$ser =  $this->ImportService->find('all',array('conditions'=>array('services_id'=>$id)));
		$this->set(compact('ser'));
		//debug($ser);
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null, $slug = null) {
		$this->Service->id = $this->params['id'];
		$this->set('saved', false);
		if (!$this->Service->exists($this->Service->id)) {
			throw new NotFoundException(__('Không có bản ghi này'));
		}
		if ($this->request->is(array('post', 'put'))) {
			$price = str_replace(',', '', $this->request->data['Service']['price']);
			//debug($price);

			$data = array();
			$data['Service']['name']= $this->request->data['Service']['name'];
			$data['Service']['price']= $price;
			$data['Service']['unit']= $this->request->data['Service']['unit'];
			$data['Service']['note']= $this->request->data['Service']['note'];
			$data['Service']['cate_services_id']= $this->request->data['Service']['cate_services_id'];
			$data['Service']['status']= $this->request->data['Service']['status'];
			$data['Service']['orders']= $this->request->data['Service']['orders'];
			$this->Service->id = $id; 
			if ($this->Service->save($data)) {
				$this->set('saved', true);
				//debug($saved);
				//$this->redirect(array('action' => 'index'));
			}
		} else {
			$options = array('conditions' => array('Service.' . $this->Service->primaryKey => $this->Service->id));
			$this->request->data = $this->Service->find('first', $options);
		}
		$cateServices = $this->Service->CateService->find('list');
		$this->set(compact('cateServices'));
		}
	/*	 
	public function edit($id = null,$slug = null) {	
		$this->set('saved', false); //false by default - controls closure of overlay in which this is opened   
		if (!$this->Service->exists($id)) {
			throw new NotFoundException(__('Không có bản ghi này'));
		}
		if ($this->request->is(array('post', 'put'))) {
			$price = str_replace(',', '', $this->request->data['Service']['price']);
			//debug($price);

			$data = array();
			$data['Service']['name']= $this->request->data['Service']['name'];
			$data['Service']['price']= $price;
			$data['Service']['unit']= $this->request->data['Service']['unit'];
			$data['Service']['note']= $this->request->data['Service']['note'];
			$data['Service']['cate_services_id']= $this->request->data['Service']['cate_services_id'];
			$data['Service']['status']= $this->request->data['Service']['status'];
			$data['Service']['orders']= $this->request->data['Service']['orders'];
			$this->Service->id = $id; 
			if ($this->Service->save($data)) {
				$this->set('saved', true);
				//debug($saved);
				//$this->redirect(array('action' => 'index'));
			}
		} else {
			$options = array('conditions' => array('Service.' . $this->Service->primaryKey => $id));
			$this->request->data = $this->Service->find('first', $options);
		}
		$cateServices = $this->Service->CateService->find('list');
		$this->set(compact('cateServices'));
	}
*/
/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null, $slug = null) {
		//$this->autoRender = false;
		$this->Service->id = $this->params['id'];
		//debug($id);
		if (!$this->Service->exists()) {
			throw new NotFoundException(__('Không có bản ghi này'));
		}
		//$this->request->allowMethod('post', 'delete');
		// check bill chưa thanh toán xem có service nào đang dùng không???
		/*
		$service_bill = $this->Service->DetailBill->find('count',array('conditions'=>array('DetailBill.services_id'=>$id,)));
		if($service_bill >0){
			$this->Session->setFlash(__('Danh mục này có chứa bản ghi con. Hãy xóa các bản ghi con trước !', true));
			$this->redirect(array('action' => 'index'));
		}else{
		*/
			if ($this->Service->delete($this->Service->id)) {
				$this->Session->setFlash(__('The service has been deleted.'));
			} else {
				$this->Session->setFlash(__('The service could not be deleted. Please, try again.'));
			}
			return $this->redirect(array('action' => 'index'));
		
	}
}
